import type { App } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import { setupRouterGuards } from './guards'
import { routes } from './routes'
import logger from '@/utils/logger'

// 创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior: (to, from, savedPosition) => {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0, behavior: 'smooth' }
    }
  }
})

export async function setupRouter(app: App) {
  // 设置路由守卫
  setupRouterGuards(router)
  
  // 注册路由
  app.use(router)
  
  // 等待路由准备就绪
  await router.isReady()
  
  logger.info('Vue Router已初始化')
}

export default router